package com.jthinker.service.impl;

import com.jthinker.bean.vo.QueryResult;
import com.jthinker.bean.vo.SearchFilter;
import com.jthinker.dao.CategoryDao;
import com.jthinker.service.CategoryService;
import com.jthinker.service.CommonService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Gang Chen
 * Date: 2015/4/22 22:12
 */
@Service
public class CategoryServiceImpl implements CategoryService
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CategoryService.class);

    @Resource
    private CategoryDao categoryDao;

    @Resource
    private CommonService commonService;

    @Override
    public Long findBySlug(String slug)
    {
        return categoryDao.findBySlug(slug);
    }

    @Override
    public boolean isSlugUnique(String slug)
    {
        return categoryDao.isSlugUnique(slug);
    }

    @Override
    public List<Map<String, Object>> findTree()
    {
        List<Map<String, Object>> roots = new ArrayList<Map<String, Object>>();

        Map<Object, Map<String, Object>> all = new HashMap<Object, Map<String, Object>>();
        List<Map<String, Object>> raw = findAll();

        for (Map<String, Object> row : raw)
        {
            if (MapUtils.getLong(row, "id").longValue() == 0l)
            {
                continue;
            }
            all.put(row.get("id"), row);
        }

        for (Map.Entry<Object, Map<String, Object>> entry : all.entrySet())
        {
            Map<String, Object> node = entry.getValue();

            //根节点
            if (node.get("parent_id") == null || MapUtils.getLong(node, "parent_id").longValue() == 0L)
            {
                roots.add(node);
            }
            else
            {
                Map<String, Object> parent = all.get(node.get("parent_id"));

                List<Map<String, Object>> children = (List<Map<String, Object>>) parent.get("children");
                if (CollectionUtils.isEmpty(children))
                {
                    children = new ArrayList<Map<String, Object>>();
                    parent.put("children", children);
                }
                children.add(node);
            }
        }

        return roots;
    }

    @Override
    public void add(Map<String, Object> record) throws Exception
    {
        try
        {
            categoryDao.add(record);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            LOGGER.error("Exception: {}", e);
        }
    }

    @Override
    public List<Map<String, Object>> findAll()
    {
        return categoryDao.findAll();
    }

    @Override
    public QueryResult<Map<String, Object>> query(SearchFilter filter)
    {
        return commonService.querySql(filter,
                "SELECT c.id,c.`name`,c.slug,c.parent_id,c2.name pname FROM blog_category c LEFT JOIN blog_category c2 ON c.parent_id=c2.id");
    }

    @Override
    public void delete(String id) throws Exception
    {
        try
        {
            categoryDao.delete(id);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            LOGGER.error("Exception: ", e);
        }
    }
}
